import Vue from 'vue'
import Vuex from 'vuex'
import * as db from '../util/db.js'
Vue.use(Vuex)
let userInfo = db.get('userInfo') || {}
let token = db.get('token') || ''
let address = db.get('address') || '未知'
const store = new Vuex.Store({
	state: {
		userInfo: userInfo,
		token: token,
		address: address
	},
	mutations: {
		saveUserTokenAction(state, token) {
			state.token = token;
			db.set('token', state.token)
		},
		setUserInfoMutations(state, userinfo) {
			state.userInfo = userinfo;
			db.set('userInfo', state.userInfo)
		},
		saveAddressAction(state, address) {
			state.address = address;
			db.set('address', state.address)
		},
		closeData(state) {
			state.userInfo = {}
			state.token = ''
			state.address = ''
			db.set('userInfo', state.userInfo)
			db.set('address', state.address)
			db.set('token', state.token)
		}
	},
	actions: {
		saveUserTokenAction(state, token) {
			state.commit('saveUserTokenAction', token)
		},
		setUserInfoAction(state, userinfo) {
			state.commit('setUserInfoMutations', userinfo)
		},
		setUserInfoAddress(state, address) {
			state.commit('saveAddressAction', address)
		}
	},
	getters: {
		getUserInfo(state) {
			return state.userInfo;
		},
		getisLogin(state) {
			if (JSON.stringify(state.userInfo) == "{}") {
				return false;
			} else {
				return true;
			}
		},
		getAddress(state) {
			return state.address;
		},
	}
})

export default store
